package br.com.edoc.controller;

import java.io.Serializable;
import java.util.Map;

import javax.el.MethodExpression;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.MethodExpressionActionListener;

import br.com.edoc.controller.portal.PortalController;

import br.com.edoc.entidade.Usuario;

import br.com.edoc.fabrica.Atendente;


import br.com.edoc.popUp.ManagerPopUp;

public abstract class ControllerPadrao implements Serializable {
	private Boolean showPopUp;
	
	public Atendente atendente;
	
	protected transient Map<String,Object> sessionScope;
	


	public Atendente getAtendente() {
		if(atendente==null){
			atendente =Atendente.getInstancia();
		}
		return atendente;
	}

	public void setAtendente(Atendente atendente) {
		this.atendente = atendente;
	}
	public Usuario getUsuarioLogado(){
		return ((PortalController) getSessionScope().get("portal")).getUsuario();
	}
	
	

	/**
	 * 
	 */
	private static final long serialVersionUID = 4866875241949234800L;
	
	private ManagerPopUp manager;

	protected void addErroMessage(String message,String detalhe){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,message,detalhe));
	}
	protected void addInfoMessage(String message,String detalhe){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,message,detalhe));
	}
	protected void addAlertMessage(String message,String detalhe){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,message,detalhe));
	}
	protected void addErroMessage(String message){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,null,message));
	}
	protected void addErroMessage(Exception e){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,null,e.getMessage()));
	}
	protected void addInfoMessage(String message){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO,null,message));
	}
	protected void addAlertMessage(Exception message){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,null,message.getMessage()));
	}
	protected void addAlertMessage(String message){
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN,null,message));
	}
	
	public void showInFormPopUp(String idComponente){
		setShowPopUp(Boolean.TRUE);
		setManager(new ManagerPopUp(idComponente,Boolean.TRUE));
	}

	public Boolean getShowPopUp() {
		Boolean value = showPopUp;
		setShowPopUp(Boolean.FALSE);
		return value;
	}

	public void setShowPopUp(Boolean showPopUp) {
		this.showPopUp = showPopUp;
	}

	public ManagerPopUp getManager() {
		return manager;
	}

	public void setManager(ManagerPopUp manager) {
		this.manager = manager;
	}
	public Map<String,Object> getSessionScope() {
		FacesContext context = FacesContext.getCurrentInstance();
		if (context != null) {
			sessionScope = (Map) context.getApplication().createValueBinding("#{sessionScope}").getValue(context);
		}
		return sessionScope;
	}

	public static MethodExpression createAction(String actionExpression, Class<?> returnType) {
	    FacesContext context = FacesContext.getCurrentInstance();
	    return context.getApplication().getExpressionFactory()
	        .createMethodExpression(context.getELContext(), actionExpression, returnType, new Class[0]);
	}

	public static MethodExpressionActionListener createActionListener(String actionListenerExpression) {
	    FacesContext context = FacesContext.getCurrentInstance();
	    return new MethodExpressionActionListener(context.getApplication().getExpressionFactory()
	        .createMethodExpression(context.getELContext(), actionListenerExpression, null, new Class[] {ActionEvent.class}));
	}


}
